package com.album.controller;


import com.album.auto.JSONAuthentication;
import com.album.entity.AlAl;
import com.album.entity.AlUser;
import com.album.mapper.AlAlMapper;
import com.album.response.Result;
import com.album.utils.UpPhotoNameUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author album
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/al-al")
public class AlAlController extends JSONAuthentication {
    @Autowired
    AlAlMapper alAlMapper;
    @ApiOperation(value = "添加相册")
    @PostMapping("/add")
    public void addAl(@RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse response,@RequestParam("name") String name,@RequestParam("userId") int userId,@RequestParam("value") String value) throws Exception {
        AlAl alAl=new AlAl();
        alAl.setName(name);
        alAl.setUserId(userId);
        alAl.setValue(value);
      QueryWrapper wrapper=new QueryWrapper();
      wrapper.eq("user_id",alAl.getUserId());
      wrapper.eq("name",alAl.getName());
      if(alAlMapper.selectOne(wrapper)!=null){
          Result error = Result.error().message("该角色已存在该名称相册");
          this.WriteJSON(request, response, error);
      }else{
          byte[] bytes = file.getBytes();
          String imageFileName = file.getOriginalFilename();
          String fileName = UpPhotoNameUtils.getPhotoName("img", imageFileName);
          Path path = Paths.get("D:\\自己的项目\\订餐系统\\order\\src\\assets\\picture\\" + fileName);
          //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
          Files.write(path, bytes);//写入文件
          String avatar_url = fileName;
          alAl.setImg(avatar_url);
          response.reset();
          if(alAlMapper.insert(alAl)>0){
              Result ok = Result.ok();
              this.WriteJSON(request, response, ok);
          }else{
              Result error = Result.error();
              this.WriteJSON(request, response, error);
          }
      }
    }
    @ApiOperation(value = "删除相册")
    @PostMapping("/del")
    public void delAl(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = alAlMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改相册介绍")
    @PostMapping("/updAl")
    public void updAl(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id, @RequestParam("value") String value) throws Exception {
        AlAl alAl = alAlMapper.selectById(id);
        alAl.setValue(value);
        if(alAlMapper.updateById(alAl)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过用户id查找用户相册")
    @PostMapping("/findByUserId")
    public void findByUserId(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId);
        Page<AlAl> page = new Page<>(size, current);
        Page<AlAl> pages = alAlMapper.selectPage(page, wrapper);
        List<AlAl> list = pages.getRecords();
        int total = alAlMapper.selectCount(wrapper);
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("用户没有相册");
        this.WriteJSON(request, response, error);
    }

}

